package com.bardpub.weblite.connect 
{
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.events.ProgressEvent;
	/**
	 * Copyright © 2010-2012, BardPub.com
	 * @fileName 
	 * @author LittleBard
	 * @version 1.0
	 * @date 
	 * @descrition 
	 * @others
	 * @history
	 */
	
	public class WebRequest
	{
		
		public static const TYPE_TXT:String = "txt";
		public static const TYPE_XML:String = "xml";
		public static const TYPE_JSON:String = "json";
		public static const TYPE_IMG:String = "img";
		public static const TYPE_SWF:String = "swf";
		
		private static var __instance:WebRequest;
		
		private static var PID:uint = 0;
		
		private var _lineholders:Array;
		
		private var _isLoading:Boolean;
		private var __singleRequest:BasicRequest;
		
		private var __curLoader:BasicRequest;
		
		/**
		 * 创建 com.bardpub.weblite.connect.WebRequest 类的新实例
		 * 禁止直接实例化，请使用WebRequest.getInstance()获取唯一实例
		 */
		public function WebRequest() 
		{
			_lineholders = new Array();
			_isLoading = false;
		}
		
		/**
		 * 获取WebRequest类的唯一实例 
		 * @return the instance of WebRequest
		 */
		public static function getInstance():WebRequest
		{
			if (__instance == null)
			{
				__instance = new WebRequest();
			}
			return __instance;
		}
		
		/**
		 * 线性载入外部资源,
		 * 如果原资源未载入完成再次调用本方法则会取消先前的载入
		 * @param	url
		 * @param	format
		 * @param	successHandler
		 * @param	progressHandler
		 * @param	failedHandler
		 */
		public function singleLoad(url:String, 
		                     format:String = "txt", 
							 successHandler:Function = null, 
							 progressHandler:Function = null,
							 failedHandler:Function = null):void
		{
			var tag:WebRequestTag = new WebRequestTag();
			tag.successHandler = successHandler;
			tag.errorHandler = failedHandler;
			tag.progressHandler = progressHandler;
			tag.url = url;
			tag.format = format;
			tag.pid = PID++;
			
			if (__singleRequest != null)
			{
				__singleRequest.close();
				__singleRequest.tag = tag;
			} 
			else 
			{
				__singleRequest = new BasicRequest(tag);
			}
			
			__singleRequest.init();
		}
		
		/**
		 * 清除线性载入
		 */
		public function cleanSingleLoad():void 
		{
			
			if (__singleRequest != null)
			{
				__singleRequest.close();
				__singleRequest = null;
			}
		}
		
		public function unload():void 
		{
			_isLoading = false;
			if (__curLoader != null)
			{
				__curLoader.close();
			}
			_lineholders = new Array();
		}

		
		/**
		 * 载入外部资源
		 * @param	url		资源URL
		 * @param	format	资源类型
		 * @param	handler	完成事件
		 * @param   queue	是否进入队列
		 */
		public function load(url:String, 
		                     format:String = "txt", 
							 successHandler:Function = null, 
							 progressHandler:Function = null,
							 failedHandler:Function = null,
							 queue:Boolean = true):void
		{
			var tag:WebRequestTag = new WebRequestTag();
			tag.successHandler = successHandler;
			tag.errorHandler = failedHandler;
			tag.progressHandler = progressHandler;
			tag.url = url;
			tag.format = format;
			tag.pid = PID++;
			tag.queue = queue;
			if (queue)
			{
				_lineholders.push(tag);
				if (!_isLoading)
				{
					loadQueue();
				}
			}
			else 
			{
				var req:BasicRequest = new BasicRequest(tag);
				req.init();
			}
		}
		
		/**
		 * 载入队列中首个资源
		 */
		private function loadQueue():void 
		{
		
			_isLoading = true;
			if (_lineholders[0] == null)
			{
				_isLoading = false;
				return;
			}
			var tag:WebRequestTag = _lineholders[0] as WebRequestTag;
			
			var req:BasicRequest = new BasicRequest(tag);
			__curLoader = req;
			req.addEventListener(Event.COMPLETE, onRequestComplete);
			req.addEventListener(IOErrorEvent.IO_ERROR, onRequestIOError);
			req.init();
		}
		
		/**
		 * 资源载入完成后自动调用
		 */
		private function onReqSingleEnd():void 
		{
			
			_lineholders.splice(0, 1);
			if (_lineholders.length > 0)
			{
				loadQueue();
			}
			else 
			{
				__curLoader = null;
				_isLoading = false;
			}
		}
		
		/**
		 * 当发生IO错误时自动触发
		 * @param	e
		 */
		private function onRequestIOError(e:IOErrorEvent):void 
		{
			var req:BasicRequest = BasicRequest(e.target);
			req.removeEventListener(Event.COMPLETE, onRequestComplete);
			req.removeEventListener(IOErrorEvent.IO_ERROR, onRequestIOError);
			onReqSingleEnd();
		}
		
		/**
		 * 当资源载入完成后触发
		 * @param	e
		 */
		private function onRequestComplete(e:Event):void 
		{
			var req:BasicRequest = BasicRequest(e.target);
			req.removeEventListener(Event.COMPLETE, onRequestComplete);
			req.removeEventListener(IOErrorEvent.IO_ERROR, onRequestIOError);
			onReqSingleEnd();
		}
		
	}

}